package bluetooth.lite.com.lightble;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.vise.baseble.BluetoothDeviceManager;
import com.vise.baseble.callback.IBleCallback;
import com.vise.baseble.core.BluetoothGattChannel;
import com.vise.baseble.exception.BleException;
import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.utils.HexUtil;

import java.util.concurrent.CountDownLatch;
import java.util.zip.ZipEntry;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    //发送同步数据demo
    public void click1(View view) {
        final String hex = "434F4E54";
        byte data[] = HexUtil.decodeHex(hex);
        byte[] rData = BluetoothDeviceManager.getInstance().syncWrite(data, 3000);
        String rHex = HexUtil.encodeHexStr(rData);
        Log.i("blexxx", rHex);
    }

    public void click2(View view) {
        BluetoothDeviceManager.getInstance().clearHardwareDevice();
    }

    public void click3(View view) {
        asyncDemo();
    }

    //异步发送接收数据demo
    private void asyncDemo() {
        final String hex = "434F4E54";
        byte data[] = HexUtil.decodeHex(hex);
        BluetoothDeviceManager.getInstance().enableReceiveData(new IBleCallback() {
            @Override
            public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) {
                String hexString = HexUtil.encodeHexStr(data);
                Log.i("receive", hexString);
            }

            @Override
            public void onFailure(BleException exception) {
                Log.i("fail", "fail");
            }
        });
        BluetoothDeviceManager.getInstance().write(data);
    }
}
